package com.xianyun.common.core.utils;

import com.alibaba.cola.exception.BizException;
import com.xianyun.common.core.config.BizDicConfig;
import com.xianyun.common.core.exception.ParamCheckException;
import java.util.List;

/**
 * 逻辑校验工具类
 * @author 朱永杰
 */
public class FilterCheckUtil {

    /***
     * 数据校验
     * @param id 唯一键
     * @param searchList SQL 查询集合
     * @param errorMsg 错误信息
     * @param <T> 封装类型
     */
    public static<T> void dataParamCheck(Long id, List<T> searchList,String errorMsg,String code,Integer index){
        if (null!=id)
        {
            if (searchList.size()> BizDicConfig.CONTAIN_ID_CHECK_LENGTH)
            {
                throwParamCheckException(index,errorMsg,code);
            }
        }else {
            if (searchList.size()> BizDicConfig.NULL_ID_CHECK_LENGTH)
            {
                throwParamCheckException(index,errorMsg,code);
            }
        }
    }

    /***
     * 数据校验
     * @param id 唯一键
     * @param searchList SQL 查询集合
     * @param errorMsg 错误信息
     * @param <T> 封装类型
     */
    public static<T> void dataCheck(Long id, List<T> searchList,String errorMsg){
        if (null!=id)
        {
            if (searchList.size()> BizDicConfig.CONTAIN_ID_CHECK_LENGTH)
            {
                throwBusinessException(errorMsg);
            }
        }else {
            if (searchList.size()> BizDicConfig.NULL_ID_CHECK_LENGTH)
            {
                throwBusinessException(errorMsg);
            }
        }
    }

    private static void throwBusinessException(String errorMsg){
        if (null == errorMsg){
            errorMsg="业务逻辑异常 请在数据校验时添加对应的错误信息！";
        }
        throw  new BizException(errorMsg);
    }

    private static void throwParamCheckException(Integer index,String errorMsg,String code){
        if (null == errorMsg){
            errorMsg="参数校验异常 请在数据校验时添加对应的错误信息！";
        }
        throw  new ParamCheckException(index,code,errorMsg);
    }
}
